#include "PrecompiledHeader.h"
#include "Gui.h"

/*--------------------------------------------------------------------------*/

Gui::Gui(const std::string& path) :
	IResource("Gui:" + path)
{
	this->m_bgbox = NULL;
	this->m_path = path;
}

/*--------------------------------------------------------------------------*/

Gui::~Gui()
{
	if (this->m_bgbox)
	{
		delete this->m_bgbox;
	}
}

/*--------------------------------------------------------------------------*/

CtrlRoot* Gui::GetRoot()
{
	return this->m_bgbox;
}

/*--------------------------------------------------------------------------*/

void Gui::LoadFromFile()
{
	std::ifstream file(this->m_path.c_str());

	AssertLogE(file.is_open() == false, "Can not open file: %s", this->m_path);

	if (file.is_open() == false)
	{
		return;
	}

	this->m_bgbox = new CtrlRoot(m_path);

	ICtrl* active = this->m_bgbox;

	while (file.good())
	{
		std::string firstWord;
		file >> firstWord;

		if (firstWord == "position")
		{
			float x, y;
			file >> x >> y;
			active->SetPosition(glm::vec2(x, y));
		}
		else if (firstWord == "size")
		{
			float x, y;
			file >> x >> y;
			active->SetSize(glm::vec2(x, y));
		}
		else if (firstWord == "attach")
		{
			uint i;
			file >> i;
			active->SetAttachedTo((ICtrl::AttachedTo) i);
		}
		else if (firstWord == "box")
		{
			std::string name;
			file >> name;

			CtrlBox* chkbox = new CtrlBox(active, name);
			active = chkbox;
		}
		else if (firstWord == "attribute")
		{
			std::string att;
			file >> att;

			if (att == "fillparent")
			{
				if (active->GetParent() != NULL)
				{
					active->SetSize(active->GetParent()->GetSize());
					active->SetPosition(glm::vec2());
				}
				else
				{
					active->SetSize(g_Settings->GetResolutionf());
					active->SetPosition(glm::vec2());
				}
			}
		}
		else if (firstWord == "checkbox")
		{
			std::string name;
			file >> name;

			CtrlCheckbox* chkbox = new CtrlCheckbox(active, name);
			active = chkbox;
		}
		else if (firstWord == "slider")
		{
			std::string name;
			file >> name;

			CtrlSlider* slider = new CtrlSlider(active, name);
			active = slider;
		}
		else if (firstWord == "button")
		{
			std::string name;
			file >> name;

			CtrlButton* button = new CtrlButton(active, name);
			active = button;
		}
		else if (firstWord == "end")
		{
			active  = active->GetParent();
		}
		else
		{
			active->ReadSpecials(file, firstWord);
		}

		if (active == NULL)
		{
			break;
		}
	}

	file.close();
}

/*--------------------------------------------------------------------------*/

void Gui::Reload()
{
	if (this->m_bgbox != NULL)
	{
		delete this->m_bgbox;
		this->m_bgbox = NULL;
	}

	this->LoadFromFile();
}

/*--------------------------------------------------------------------------*/

const std::string& Gui::GetPath() const
{
	return this->m_path;
}

/*--------------------------------------------------------------------------*/

void Gui::SetPath(const std::string& path)
{
	this->m_path = path;
}

/*--------------------------------------------------------------------------*/